/**
 * @version Create on 2012-11-2
 * @author Yinzi Chen
 */

public class LongestPalindromicSubstring {

	public String longestPalindrome(String s) {
		int n = s.length();
		if (n == 0)
			return "";
		int p = 0, q = 0;
		for (int i = 0; i < n; ++i) {
			int l = i, r = i;
			while (l - 1 >= 0 && r + 1 < n
					&& s.charAt(l - 1) == s.charAt(r + 1)) {
				l--;
				r++;
			}
			if (r - l > q - p) {
				q = r;
				p = l;
			}
			l = i;
			r = i + 1;
			while (l >= 0 && r < n && s.charAt(l) == s.charAt(r)) {
				l--;
				r++;
			}
			if (r - l - 2 > q - p) {
				q = r - 1;
				p = l + 1;
			}
		}
		return s.substring(p, q + 1);
	}

	public static void main(String[] args) {
		LongestPalindromicSubstring a = new LongestPalindromicSubstring();
		System.out.println(a.longestPalindrome("abcdcb"));
	}

}
